unit clsRegrasPagto;

interface

uses
  Dm, dbtables, biblio, sysutils;

type
  TRegrasPagto = class(TObject)
  private
    Reg_codigo: string;
  public
    Reg_descricao: string;
    Reg_numpgto: integer;
    Reg_posicaoipi: Double;
    Reg_Ativo: string;
    Reg_PosicaoIcmsSubst: Double;
    Reg_SomaValor: string;
    function Gravar(p_Reg_codigo: string): Byte;
    function Recuperar(p_Reg_codigo: string): Byte;
    function Excluir(p_Reg_codigo: string): Byte;
    function Ultimo: string;
    procedure Calcula_Divisores(p_Reg_Codigo: string; var divisor1, divisor2: integer);
    procedure Consultar(var p_q: TQuery; p_nome: string; xmostra: boolean);
    procedure Consultar1(var p_q: TQuery; p_nome: string; xmostra: boolean);
    function Retorna_PM(p_Reg_Codigo: string): string;
  end;

implementation

function TRegrasPagto.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := dmint.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('SELECT * FROM RegrasPagto where Reg_codigo=:k_Reg_codigo');
  tq.ParamByName('k_Reg_codigo').AsString := strzero(p_Reg_codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.SQL.Clear;
    tq.Params.Clear;
    tq.SQL.Add('INSERT INTO RegrasPagto ' +
      'VALUES(:k_Reg_codigo, :k_Reg_descricao, :k_Reg_numpgto, ' +
      '       :K_Reg_Posicaoipi, :K_Reg_Ativo, :k_Reg_PosicaoIcmsSubst, ' +
      '       :k_Reg_SomaValor)');
  end else begin
    tq.SQL.Clear;
    tq.Params.Clear;
    tq.SQL.Add('UPDATE RegrasPagto ' +

      'SET Reg_descricao = :k_Reg_descricao, ' +
      '    Reg_numpgto = :k_Reg_numpgto, ' +
      '    Reg_posicaoipi = :K_Reg_posicaoipi, ' +
      '    Reg_Ativo = :K_Reg_Ativo, ' +
      '    Reg_PosicaoIcmsSubst = :k_Reg_PosicaoIcmsSubst, ' +
      '    Reg_SomaValor = :k_Reg_SomaValor ' +

      'WHERE Reg_codigo = :k_Reg_codigo');
  end;
  tq.ParamByName('k_Reg_codigo').AsString := strzero(p_Reg_codigo, 3);
  tq.ParamByName('k_Reg_descricao').AsString := Reg_descricao;
  tq.ParamByName('k_Reg_numpgto').AsFloat := Reg_numpgto;
  tq.ParamByName('K_Reg_Posicaoipi').AsFloat := Reg_Posicaoipi;
  tq.ParamByName('K_Reg_Ativo').AsString := Reg_Ativo;
  tq.ParamByName('K_Reg_PosicaoIcmsSubst').AsFloat := Reg_PosicaoIcmsSubst;
  tq.ParamByName('K_Reg_SomaValor').AsString := Reg_SomaValor;
  tq.close;
  tq.execSQL;
  result := 1;
  tq.close;
  tq.Free;
end;

function TRegrasPagto.Recuperar;
var
  tq: TQuery;

begin
  tq := Tquery.Create(dmint);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.SQL.Add('SELECT * FROM RegrasPagto where Reg_codigo=:k_Reg_codigo');
  tq.Params[0].AsString := strzero(p_Reg_codigo, 3);
  tq.open;
  Reg_codigo := p_Reg_codigo;
  if tq.eof then begin
    Reg_descricao := '';
    Reg_numpgto := 0;
    Reg_posicaoipi := 0;
    Reg_Ativo := '1';
    Result := 0;

  end else begin
    Reg_descricao := tq.FieldByName('Reg_descricao').AsString;
    Reg_numpgto := tq.FieldByName('Reg_numpgto').asinteger;
    Reg_posicaoIpi := tq.FieldByName('REg_posicaoIpi').AsFloat;
    Reg_Ativo := tq.FieldByName('Reg_Ativo').AsString;
    Reg_PosicaoIcmsSubst := tq.FieldByName('Reg_PosicaoIcmsSubst').AsFloat;
    Reg_SomaValor := tq.FieldByName('Reg_SomaValor').AsString;
    Result := 1;

  end;

  tq.Close;
  tq.Free;

end;

function TRegrasPagto.Excluir;
var
  tq: TQuery;

begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.close;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('DELETE FROM RegrasPagto WHERE Reg_codigo=:k_Reg_codigo');
  tq.ParamByName('k_Reg_codigo').AsString := StrZero(p_Reg_codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;

end;

procedure TRegrasPagto.Consultar;
begin
  p_q.SQL.Clear;
  p_q.Params.Clear;
  p_q.SQL.Add('Select distinct r.*, irg_Controle from RegrasPagto R, ItemRegra I ' +
    'where (R.Reg_Codigo = I.Reg_Codigo(+)) and Reg_descricao like :k_Reg_descricao ');

  if not xmostra then begin
    p_q.SQL.Add('and reg_ativo = ' + QuotedStr('1') + '');
  end;

  p_q.SQL.Add(' order by Reg_descricao');
  p_q.ParamByName('k_Reg_descricao').AsString := '%' + p_nome + '%';

end;

procedure TRegrasPagto.Calcula_Divisores;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from ItemRegra where Reg_codigo=:a');
  tq.Params[0].AsString := StrZero(p_Reg_codigo, 3);
  tq.open;
  divisor1 := 0;
  divisor2 := 0;
  while not tq.eof do begin
    if tq.FieldByName('Irg_Controle').AsString <> '*' then begin
      divisor1 := divisor1 + 1;
    end else begin
      //divisor1 := divisor1 + 1;
      divisor2 := divisor2 + 1;
    end;
    tq.next;
  end;
  tq.close;
  tq.Free;
end;

function TRegrasPagto.Ultimo: string;
var
  tq: TQuery;

begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select max(Reg_codigo) as ultimo FROM RegrasPagto');
  tq.open;

  if tq.FieldByName('ultimo').AsString = '' then begin
    Result := '1';
  end else begin
    Result := IntToStr(tq.FieldByName('ultimo').asinteger + 1);
  end;

  tq.close;
  tq.Free;

end;

function TRegrasPagto.Retorna_PM(p_Reg_Codigo: string): string;
var
  tq: TQuery;
  numPgto: Integer;
  Dias: Integer;

begin
  tq := Tquery.Create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.SQL.Add('Select * from ItemRegra where Reg_codigo=:a');
  tq.Params[0].AsString := StrZero(p_Reg_codigo, 3);
  tq.open;
  numPgto := 0;
  Dias := 0;
  while not tq.Eof do begin
    inc(numPgto);
    Dias := Dias + tq.FieldByName('irg_dias').AsInteger;
    tq.Next;
  end;
  Result := '0';
  tq.First;
  if not tq.Eof then begin
    Result := IntToStr(Dias div numPgto);
  end;
  tq.Close;
  tq.Free;
end;

procedure TRegrasPagto.Consultar1(var p_q: TQuery; p_nome: string; xmostra: boolean);
begin
  p_q.SQL.Clear;
  p_q.Params.Clear;
  p_q.SQL.Add('Select * from RegrasPagto where Reg_descricao = :k_Reg_descricao ');
  if not xmostra then begin
    p_q.SQL.Add('and reg_ativo = ' + QuotedStr('1') + '');
  end;
  p_q.SQL.Add(' order by Reg_descricao');
  p_q.ParamByName('k_Reg_descricao').AsString := p_nome;
end;

end.
